#include "stackeddialog.h"

// if searching both record lists, disable the category dropdown menu/combobox

StackedDialog::StackedDialog(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);

    searchOptionType = "All"; // set to ALL by default
    searchOptionCategory = "All";
    searchOptionField = "All Fields";

    reportOptionDate = "All Time";
    reportOptionFromPrice = reportOptionToPrice = 0;
    reportOptionType = "All";

    setWindowFlags(Qt::WindowTitleHint | Qt::WindowMinimizeButtonHint);
    connect(searchField, SIGNAL(currentIndexChanged(QString)), this, SLOT(setField(QString)));
    connect(searchField, SIGNAL(currentIndexChanged(QString)), searchButton, SLOT(setFocus()));
    connect(accountAlertBalance, SIGNAL(valueChanged(double)), this, SLOT(check()));
    connect(accountAlertBalance, SIGNAL(valueChanged(double)), searchButton, SLOT(setFocus()));
    connect(searchType, SIGNAL(currentIndexChanged(QString)), this, SLOT(setType(QString)));
    connect(searchType, SIGNAL(currentIndexChanged(QString)), searchButton, SLOT(setFocus()));
    connect(searchCategory, SIGNAL(currentIndexChanged(QString)), this, SLOT(setCategory(QString)));
    connect(searchCategory, SIGNAL(currentIndexChanged(QString)), searchButton, SLOT(setFocus()));
    connect(searchFromDate, SIGNAL(dateTimeChanged(QDateTime)), this, SLOT(dateConstraint()));
    connect(searchFromDate, SIGNAL(dateTimeChanged(QDateTime)), searchButton, SLOT(setFocus()));
    connect(searchToDate, SIGNAL(dateTimeChanged(QDateTime)), searchButton, SLOT(setFocus()));
    connect(searchFromPrice, SIGNAL(valueChanged(double)), searchButton, SLOT(setFocus()));
    connect(searchFromPrice, SIGNAL(valueChanged(double)), this, SLOT(priceConstraint()));
    connect(searchToPrice, SIGNAL(valueChanged(double)), searchButton, SLOT(setFocus()));
    connect(displayExpenseList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(openExpenseRecord(QListWidgetItem*)));
    connect(displayIncomeList, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(openIncomeRecord(QListWidgetItem*)));
    connect(searchExpenseResults, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(openExpenseRecord(QListWidgetItem*)));
    connect(searchIncomeResults, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(openIncomeRecord(QListWidgetItem*)));
    connect(reportPriceToggle, SIGNAL(toggled(bool)), reportToPrice, SLOT(setDisabled(bool)));
    connect(reportPriceToggle, SIGNAL(toggled(bool)), createReportButton, SLOT(setFocus()));
    connect(reportPriceToggle, SIGNAL(toggled(bool)), reportFromPrice, SLOT(setDisabled(bool)));
    connect(reportPriceToggle, SIGNAL(toggled(bool)), createReportButton, SLOT(setFocus()));
    connect(reportFromPrice, SIGNAL(valueChanged(double)), this, SLOT(priceConstraint()));
    connect(reportDateToggle, SIGNAL(toggled(bool)), reportDateRange, SLOT(setDisabled(bool)));
    connect(reportDateToggle, SIGNAL(toggled(bool)), createReportButton, SLOT(setFocus()));
    connect(reportType, SIGNAL(currentIndexChanged(QString)), this, SLOT(updateReportType(QString)));
    connect(reportType, SIGNAL(currentIndexChanged(QString)), createReportButton, SLOT(setFocus()));
    connect(reportCategoryToggle, SIGNAL(toggled(bool)), this, SLOT(updateReportCategoryAll(bool)));
    connect(reportCategoryToggle, SIGNAL(toggled(bool)), createReportButton, SLOT(setFocus()));
    connect(reportExpenseCategoryList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(setExpenseChecked(QListWidgetItem*)));
    connect(reportIncomeCategoryList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(setIncomeChecked(QListWidgetItem*)));
    connect(reportExpenseCategoryList, SIGNAL(itemClicked(QListWidgetItem*)), createReportButton, SLOT(setFocus()));
    connect(reportIncomeCategoryList, SIGNAL(itemClicked(QListWidgetItem*)), createReportButton, SLOT(setFocus()));

    connect(logoutButton, SIGNAL(clicked()), this, SLOT(saveToXML()));
    QSize size(120, 256);

    //Intitializing sizes of the display windows
    displayExpenseListCost->setFixedSize(size);
    displayExpenseListDate->setFixedSize(size);
    displayExpenseList->setFixedSize(300, 256);
    displayExpenseListCategory->setFixedSize(size);
    displayIncomeListCost->setFixedSize(size);
    displayIncomeListDate->setFixedSize(size);
    displayIncomeList->setFixedSize(300,256);
    displayIncomeListCategory->setFixedSize(size);

    accountBalance->setDisabled(true);
    accountExpense->setDisabled(true);
    accountIncome->setDisabled(true);

    //Setting initial states of display fields to disabled


    searchCategory->setDisabled(true); // set disabled by default since default search list is for both, hence no category can be selected.

    loadFromXML();

    reloadExpenseList();
    reloadIncomeList();
    accountName->setText(QString::fromStdString(accountname));
    this->timer = startTimer(500);
    accountAlertBalance->setValue(200.00);
    layout() -> setSizeConstraint(QLayout::SetFixedSize);
}

void StackedDialog::on_addIncomeRecordButton_clicked(){
    int i = 0;
    this -> addRecord = new AddRecord();
    for(i = 0; i < incomeRecords.categorysize(); i ++){
        addRecord->categoryCombo->addItem(incomeRecords.getcategory(i));
    }
    connect(this->addRecord, SIGNAL(sendRecord(QDateTime, string, string, string, double)), this, SLOT(enableWindow()));
    connect(this->addRecord, SIGNAL(on_recordCancelButton_clicked()), this, SLOT(enableWindow()));
    connect(this->addRecord, SIGNAL(sendRecord(QDateTime, string, string, string, double)),
            this, SLOT(saveIncome(QDateTime, string, string, string, double)));
    connect(this->addRecord, SIGNAL(sendCategory(string)), this, SLOT(addIncomeCategory(string)));
    connect(this->addRecord, SIGNAL(removeCategory(string)), this, SLOT(removeIncomeCategory(string)));
    this->setDisabled(true);
    addRecord -> show();
}
void StackedDialog::on_addExpenseRecordButton_clicked(){
    this -> addRecord = new AddRecord();
    int i = 0;
    for(i = 0; i < expenseRecords.categorysize(); i ++){
        addRecord->categoryCombo->addItem(expenseRecords.getcategory(i));
    }
    connect(this->addRecord, SIGNAL(sendRecord(QDateTime, string, string, string, double)), this, SLOT(enableWindow()));
    connect(this->addRecord, SIGNAL(on_recordCancelButton_clicked()), this, SLOT(enableWindow()));
    connect(this->addRecord, SIGNAL(sendRecord(QDateTime, string, string, string, double)),
            this, SLOT(saveExpense(QDateTime, string, string, string, double)));
    connect(this->addRecord, SIGNAL(sendCategory(string)), this, SLOT(addExpenseCategory(string)));
    connect(this->addRecord, SIGNAL(removeCategory(string)), this, SLOT(removeExpenseCategory(string)));
    this->setDisabled(true);
    addRecord -> show();
}

void StackedDialog::on_createReportButton_clicked(){
    // let program pass arguments to the report about what it should display

    reportOptionType = reportType->currentText().toStdString();

    if(!reportDateToggle->isChecked())
        reportOptionDate = reportDateRange->currentText().toStdString();
    else
        reportOptionDate = "All Time";

    if(!reportPriceToggle->isChecked()){
        reportOptionToPrice = reportToPrice->value();
        reportOptionFromPrice = reportFromPrice->value();
    }
    else
        reportOptionToPrice = reportOptionFromPrice = 0;

    this->setDisabled(true);
    this->report = new Report(expenseRecords,incomeRecords,reportOptionIncomeCategory, reportOptionExpenseCategory,
                              reportOptionType, reportOptionDate, reportOptionToPrice, reportOptionFromPrice);

    connect(this->report, SIGNAL(on_reportCloseButton_clicked()), this, SLOT(enableWindow()));
    report->show();
}

void StackedDialog::setField(QString string){
    this->searchOptionField = this->searchField->currentText().toStdString();

    if(string == "Price Range"){
        searchFromPrice->setDisabled(false);
        searchToPrice->setDisabled(false);
        additionalInfoField -> setDisabled(true);
        searchFromDate->setDisabled(true);
        searchToDate->setDisabled(true);
    }
    else if(string == "Date Range"){
        additionalInfoField->setDisabled(true);
        searchFromPrice->setDisabled(true);
        searchToPrice->setDisabled(true);
        searchFromDate->setDisabled(false);
        searchToDate->setDisabled(false);
    }
    else if(string == "Description" || string == "Name"){
        additionalInfoField->setDisabled(false);
        searchFromPrice->setDisabled(true);
        searchToPrice->setDisabled(true);
        searchFromDate->setDisabled(true);
        searchToDate->setDisabled(true);
    }
    else if(string == "All Fields"){
        additionalInfoField->setDisabled(false);
        searchFromPrice->setDisabled(false);
        searchToPrice->setDisabled(false);
        searchFromDate->setDisabled(false);
        searchToDate->setDisabled(false);
    }
}
void StackedDialog::setType(QString type){

    this->searchOptionType = searchType->currentText().toStdString();
    int i = 0;

    if(type == "All"){
        searchCategory->setDisabled(true);
        searchOptionCategory = "None";
    }
    else {
        searchCategory->setDisabled(false);
        searchCategory->clear();
        searchCategory->addItem("All");
        if(type == "Income"){
            for(i = 0; i < incomeRecords.categorysize(); i++)
                searchCategory->addItem(incomeRecords.getcategory(i));
        }
        if(type == "Expense"){
            for(i = 0; i < expenseRecords.categorysize(); i++)
                searchCategory->addItem(expenseRecords.getcategory(i));
        }
    }
}
void StackedDialog::setCategory(QString string){
    searchOptionCategory = string.toStdString();
}

void StackedDialog::addExpenseCategory(string input){
    int i = 0;
    expenseRecords.addCategory(QString::fromStdString(input));
    // after adding new category, update the list in addrecord
    addRecord->categoryCombo->clear();
    for(i = 0; i < expenseRecords.categorysize(); i++){
        addRecord->categoryCombo->addItem(expenseRecords.getcategory(i));
    }
    reloadExpenseList();
}
void StackedDialog::addIncomeCategory(string input){
    int i = 0;
    incomeRecords.addCategory(QString::fromStdString(input));
    addRecord->categoryCombo->clear();
    // after adding new category, update the list in addrecord
    for(i = 0; i < incomeRecords.categorysize(); i++){
        addRecord->categoryCombo->addItem(incomeRecords.getcategory(i));
    }
    reloadIncomeList();
}
void StackedDialog::openExpenseRecord(QListWidgetItem* item){
    int i = 0;
    int index = 0;
    this -> recordInfo = new RecordInfo(searchRecords(item->text(), this->expenseRecords));
    connect(this->recordInfo, SIGNAL(on_recordInfoCancel_clicked()), this, SLOT(enableWindow()));
    connect(this->recordInfo,SIGNAL(on_recordInfoAccept_clicked()), this, SLOT(enableWindow()));
    connect(this->recordInfo, SIGNAL(remove(string)), this, SLOT(removeExpense(string)));
    connect(this->recordInfo, SIGNAL(modify(Record, Record)),
            this, SLOT(modifyExpense(Record, Record)));
    this->setDisabled(true);
    for(i = 0; i < expenseRecords.categorysize(); i++){
        this->recordInfo->recordInfoCategoryCombo->addItem(expenseRecords.getcategory(i));
    }
    index = this->recordInfo->recordInfoCategoryCombo->findText(QString::fromStdString(searchRecords(item->text(), this->expenseRecords).getcategory()));
    if(index == -1){
        this->recordInfo->recordInfoCategoryCombo->addItem(QString::fromStdString(searchRecords(item->text(), this->expenseRecords).getcategory()));
        index = this->recordInfo->recordInfoCategoryCombo->findText(QString::fromStdString(searchRecords(item->text(), this->expenseRecords).getcategory()));
    }
    this->recordInfo->recordInfoCategoryCombo->setCurrentIndex(index);
    recordInfo -> show();
}
void StackedDialog::openIncomeRecord(QListWidgetItem *item){
    int i = 0;
    int index = 0;
    this -> recordInfo = new RecordInfo(searchRecords(item->text(), this->incomeRecords));
    connect(this->recordInfo, SIGNAL(on_recordInfoCancel_clicked()), this, SLOT(enableWindow()));
    connect(this->recordInfo,SIGNAL(on_recordInfoAccept_clicked()), this, SLOT(enableWindow()));
    connect(this->recordInfo, SIGNAL(modify(Record, Record)),
            this, SLOT(modifyIncome(Record, Record)));
    connect(this->recordInfo, SIGNAL(remove(string)), this, SLOT(removeIncome(string)));
    this->setDisabled(true);
    this->recordInfo->recordInfoCategoryCombo->clear();
    for(i = 0; i < incomeRecords.categorysize(); i++){
        this->recordInfo->recordInfoCategoryCombo->addItem(incomeRecords.getcategory(i));
    }
    index = this->recordInfo->recordInfoCategoryCombo->findText(QString::fromStdString(searchRecords(item->text(), this->incomeRecords).getcategory()));

    if(index == -1){
        this->recordInfo->recordInfoCategoryCombo->addItem(QString::fromStdString(searchRecords(item->text(), this->incomeRecords).getcategory()));
        index = this->recordInfo->recordInfoCategoryCombo->findText(QString::fromStdString(searchRecords(item->text(), this->incomeRecords).getcategory()));
    }
    this->recordInfo->recordInfoCategoryCombo->setCurrentIndex(index);
    recordInfo -> show();
}

void StackedDialog::enableWindow(){
    this->setEnabled(true);
    reloadIncomeList();
    reloadExpenseList();
}
void StackedDialog::saveToXML(){
    this->tree.saveList2XML(expenseRecords, incomeRecords, alertList,accountname);
    this->close();
}
void StackedDialog::loadFromXML(){
    this->tree.loadXML2List(expenseRecords, incomeRecords, alertList, accountname);
}

Record StackedDialog::searchRecords(const QString string, vector<Record> recordList){
    int i = 0;
    for(i = 0; i < recordList.size(); i++){
        if(QString::fromStdString(recordList[i].getnameid()) == string)
            return recordList[i];
    }
}

Record StackedDialog::searchRecords(const QString string, ExpenseRecordList recordList){
    int i = 0;
    for(i = 0; i < recordList.size(); i++){
        if(QString::fromStdString(recordList[i].getnameid()) == string)
            return recordList[i];
    }
}
Record StackedDialog::searchRecords(const QString string, IncomeRecordList recordList){
    int i = 0;
    for(i = 0; i < recordList.size(); i++){
        if(QString::fromStdString(recordList[i].getnameid()) == string)
            return recordList[i];
    }
}
double StackedDialog::calculateBalance(){
    balance = (income = incomeRecords.calculateIncome()) - (expense = expenseRecords.calculateExpense());
    return balance;
}
int StackedDialog::currentTimeInSeconds(){
    int current_time;
    QDateTime now = QDateTime::currentDateTime();
    QDateTime xmas(QDate(1000, 12, 25), QTime(0, 0));
    return current_time;
}
void StackedDialog::saveIncome(QDateTime datetime, string info, string name, string category, double amount){
    incomeRecords.addRecord(Record(datetime,info, name, category,amount));
    processor.process(add_Record, Record(datetime,info, name, category,amount),1);
    reloadIncomeList();
}
void StackedDialog::saveExpense(QDateTime datetime, string info, string name, string category, double amount){
    expenseRecords.addRecord(Record(datetime, info, name, category,amount));
    processor.process(add_Record, Record(datetime, info, name, category,amount),0);
    reloadExpenseList();
}

void StackedDialog::reloadExpenseList(){
    QString number;
    int i = 0;
    displayExpenseList->clear();
    displayExpenseListCost->clear();
    displayExpenseListDate->clear();
    displayExpenseListCategory->clear();
    searchExpenseResults->clear();
    for(i = 0; i < expenseRecords.size(); i++){
        number.setNum(expenseRecords[i].getamount());
        displayExpenseList->addItem(QString::fromStdString(expenseRecords[i].getnameid()));
        displayExpenseListCost->addItem(number);
        displayExpenseListDate->addItem(expenseRecords[i].sgetdatetime());
        displayExpenseListCategory->addItem(QString::fromStdString(expenseRecords[i].getcategory()));
    }

    checkBalance();
    accountIncome->setText(number.setNum(income));
    accountExpense->setText(number.setNum(expense));
    accountBalance->setText(number.setNum(balance));

    reportOptionExpenseCategory.clear();
    reportExpenseCategoryList->clear();

    for(i = 0; i < expenseRecords.categorysize(); i++){
        QListWidgetItem* item = new QListWidgetItem(expenseRecords.getcategory(i));
        item->setFlags(item->flags() & Qt::ItemIsUserCheckable);
        reportExpenseCategoryList->addItem(item);
    }

}
void StackedDialog::reloadIncomeList(){
    QString number;
    int i = 0;
    displayIncomeList->clear();
    displayIncomeListCost->clear();
    displayIncomeListDate->clear();
    displayIncomeListCategory->clear();
    searchIncomeResults->clear();
    for(i = 0; i < incomeRecords.size(); i++){
        number.setNum(incomeRecords[i].getamount());
        displayIncomeList->addItem(QString::fromStdString(incomeRecords[i].getnameid()));
        displayIncomeListCost->addItem(number);
        displayIncomeListDate->addItem(incomeRecords[i].sgetdatetime());
        displayIncomeListCategory->addItem(QString::fromStdString(incomeRecords[i].getcategory()));
    }
    checkBalance();
    accountIncome->setText(number.setNum(income));
    accountExpense->setText(number.setNum(expense));
    accountBalance->setText(number.setNum(balance));

    reportOptionIncomeCategory.clear();
    reportIncomeCategoryList->clear();

    for(i = 0; i < incomeRecords.categorysize(); i++){
        QListWidgetItem* item = new QListWidgetItem(incomeRecords.getcategory(i));
        item->setFlags(item->flags() & Qt::ItemIsUserCheckable);
        reportIncomeCategoryList->addItem(item);
    }
}
void StackedDialog::removeIncome(string id){
    processor.process(delete_Record, searchRecords(QString::fromStdString(id), incomeRecords),1);
    Record r = Record();
    r.setnameid(id);
    incomeRecords.deleteRecord(r);
    this->setEnabled(true);
    reloadIncomeList();
}
void StackedDialog::removeExpense(string id){
    processor.process(delete_Record, searchRecords(QString::fromStdString(id), expenseRecords),0);
    Record r = Record();
    r.setnameid(id);
    expenseRecords.deleteRecord(r);
    this->setEnabled(true);
    reloadExpenseList();
}
void StackedDialog::on_searchButton_clicked(){
    int i = 0;
    searchInput = additionalInfoField->text().toStdString();
    expenseResults.clear();
    incomeResults.clear();
    if(searchOptionType == "Expense"){
        searchExpenseResults->setDisabled(false);
        searchIncomeResults->setDisabled(true);
        expenseResults = expenseRecords.searchRecord(searchInput, searchOptionCategory, searchOptionField,
                                                     searchFromDate->dateTime(), searchToDate->dateTime(),
                                                     searchFromPrice->value(), searchToPrice->value());
    }
    else if(searchOptionType == "Income"){
        searchExpenseResults->setDisabled(true);
        searchIncomeResults->setDisabled(false);
        incomeResults = incomeRecords.searchRecord(searchInput,searchOptionCategory, searchOptionField,
                                                   searchFromDate->dateTime(), searchToDate->dateTime(),
                                                   searchFromPrice->value(), searchToPrice->value());
    }
    else if(searchOptionType == "All"){
        searchOptionCategory = "All";
        searchExpenseResults->setDisabled(false);
        searchIncomeResults->setDisabled(false);
        incomeResults = incomeRecords.searchRecord(searchInput,searchOptionCategory, searchOptionField,
                                                   searchFromDate->dateTime(), searchToDate->dateTime(),
                                                   searchFromPrice->value(), searchToPrice->value());
        expenseResults = expenseRecords.searchRecord(searchInput,searchOptionCategory, searchOptionField,
                                                     searchFromDate->dateTime(), searchToDate->dateTime(),
                                                     searchFromPrice->value(), searchToPrice->value());
    }
    searchExpenseResults->clear();
    searchIncomeResults->clear();
    for(i = 0; i < expenseResults.size(); i++){
        searchExpenseResults->addItem(QString::fromStdString(expenseResults[i].getnameid()));
    }
    for(i = 0; i < incomeResults.size(); i++){
        searchIncomeResults->addItem(QString::fromStdString(incomeResults[i].getnameid()));
    }
}
void StackedDialog::timerEvent(QTimerEvent * event){
    checkAlert();
}
void StackedDialog::checkBalance(){
    if(calculateBalance() > 0 && calculateBalance() < accountAlertBalance->value())
        this->setStyleSheet("QListWidget#accountStatus{background-color: yellow}");
    else if(calculateBalance() < 0)
        this->setStyleSheet("QListWidget#accountStatus{background-color: red}");
    else
        this->setStyleSheet("QListWidget#accountStatus{background-color: white}");
}
void StackedDialog::check(){
    checkBalance();
}
void StackedDialog::modifyExpense(Record oldRecord, Record newRecord){
    processor.process(modify_Record, oldRecord, newRecord,0);
    expenseRecords.modifyRecord(oldRecord, newRecord);
    reloadExpenseList();
}

void StackedDialog::modifyIncome(Record oldRecord, Record newRecord){
    incomeRecords.modifyRecord(oldRecord, newRecord);
    processor.process(modify_Record, oldRecord, newRecord,1);
    reloadIncomeList();
}

void StackedDialog::removeIncomeCategory(string category){
    int i= 0;
    incomeRecords.removeCategory(QString::fromStdString(category));
    addRecord->categoryCombo->clear();
    for(i = 0; i < incomeRecords.categorysize(); i++){
        addRecord->categoryCombo->addItem(incomeRecords.getcategory(i));
    }
}

void StackedDialog::removeExpenseCategory(string category){
    int i = 0;
    expenseRecords.removeCategory(QString::fromStdString(category));
    addRecord->categoryCombo->clear();
    for(i = 0; i < expenseRecords.categorysize(); i++){
        addRecord->categoryCombo->addItem(expenseRecords.getcategory(i));
    }
}
void StackedDialog::on_incomeUndoButton_clicked(){
    processor.process(undo_Record);
    this->expenseRecords = processor.getExpenseList();
    this->incomeRecords = processor.getIncomeList();
    reloadIncomeList();
    reloadExpenseList();
}

void StackedDialog::on_expenseUndoButton_clicked(){
    processor.process(undo_Record);
    this->expenseRecords = processor.getExpenseList();
    this->incomeRecords = processor.getIncomeList();
    reloadIncomeList();
    reloadExpenseList();
}

void StackedDialog::on_manageAlertButton_clicked(){

    int i = 0;
    manageAlert = new ManageAlert();
    manageAlert->setAlertList(alertList);
    connect(manageAlert, SIGNAL(updateAlertList(AlertList)), this, SLOT(updateAlertList(AlertList)));
    connect(manageAlert, SIGNAL(updateAlertList(AlertList)), this, SLOT(enableWindow()));
    connect(manageAlert, SIGNAL(close()), this, SLOT(enableWindow()));
    for(i = 0; i < alertList.size(); i++){
        manageAlert->manageAlertNameID->addItem(QString::fromStdString(alertList[i].getnameid()));
    }for(i = 0; i < alertList.size(); i++)
        manageAlert->manageAlertDateTime->addItem(alertList[i].sgetdatetime());
    this->setDisabled(true);
    manageAlert->show();
}
void StackedDialog::updateAlertList(AlertList alertList){
    this->alertList = alertList;
}

void StackedDialog::checkAlert(){
    int i = 0;
    for(i = 0; i < alertList.size(); i++){
        if(alertList[i].getdisabled() == false){
            if(alertList[i].getrecurring() == true){
                if(alertList[i].getfrequency() == "Daily" && alertList[i].getdatetime().time() == QDateTime::currentDateTime().time()){
                    QMessageBox::warning(this,QString::fromStdString(alertList[i].getname()),QString::fromStdString(alertList[i].getinfo()),
                                         QMessageBox::Ok);
                }
                if(alertList[i].getfrequency() == "Weekly" && alertList[i].getdatetime().time() == QDateTime::currentDateTime().time() &&
                        alertList[i].getdatetime().date().dayOfWeek() == QDateTime::currentDateTime().date().dayOfWeek()){
                    QMessageBox::warning(this,QString::fromStdString(alertList[i].getname()),QString::fromStdString(alertList[i].getinfo()),
                                         QMessageBox::Ok);
                }
                if(alertList[i].getfrequency() == "Monthly" && alertList[i].getdatetime().time() == QDateTime::currentDateTime().time() &&
                        alertList[i].getdatetime().date().day() == QDateTime::currentDateTime().date().day()){
                    QMessageBox::warning(this,QString::fromStdString(alertList[i].getname()),QString::fromStdString(alertList[i].getinfo()),
                                         QMessageBox::Ok);
                }
            }
            else if(!alertList[i].getactivated()){
                QMessageBox::warning(this,QString::fromStdString(alertList[i].getname()),QString::fromStdString(alertList[i].getinfo()),
                                     QMessageBox::Ok);
                Alert a;
                a = alertList[i];
                a.setactivated(true);
                alertList.modifyAlert(alertList[i],a);
            }
        }
    }
}
void StackedDialog::updateReportType(QString input){
    reportOptionType = input.toStdString();
    if(input == "Expense"){
        reportIncomeCategoryList->setDisabled(true);
        reportExpenseCategoryList->setDisabled(false);
    }
    else if(input == "Income"){
        reportExpenseCategoryList->setDisabled(true);
        reportIncomeCategoryList->setDisabled(false);
    }
    else if(input == "All"){
        reportExpenseCategoryList->setDisabled(false);
        reportIncomeCategoryList->setDisabled(false);
    }
}

void StackedDialog::updateReportCategoryAll(bool input){
    int i = 0;

    reportOptionExpenseCategory.clear();
    for(i = 0; reportExpenseCategoryList->item(i) != NULL; i++){
        if(input){
            reportExpenseCategoryList->item(i)->setCheckState(Qt::Checked);
            reportOptionExpenseCategory.push_back(reportExpenseCategoryList->item(i)->text());}
        else
            reportExpenseCategoryList->item(i)->setCheckState(Qt::Unchecked);
    }
    reportOptionIncomeCategory.clear();
    for(i = 0; reportIncomeCategoryList->item(i) != NULL; i++){
        if(input){
            reportIncomeCategoryList->item(i)->setCheckState(Qt::Checked);
            reportOptionIncomeCategory.push_back(reportIncomeCategoryList->item(i)->text());
        }
        else
            reportIncomeCategoryList->item(i)->setCheckState(Qt::Unchecked);
    }
}

void StackedDialog::setExpenseChecked(QListWidgetItem* item){
    if(item->checkState()){
        item->setCheckState(Qt::Unchecked);
        int i = 0;
        for(i = 0; i < reportOptionExpenseCategory.size(); i++)
            if(reportOptionExpenseCategory[i] == item->text())
                reportOptionExpenseCategory.erase(reportOptionExpenseCategory.begin() + i);
    }
    else{
        int i = 0;
        bool exist = false;
        item->setCheckState(Qt::Checked);
        for(i = 0; i < reportOptionExpenseCategory.size(); i++)
            if(item->text() == reportOptionExpenseCategory[i])
                exist = true;
        if(!exist)
            reportOptionExpenseCategory.push_back(item->text());
    }
}
void StackedDialog::setIncomeChecked(QListWidgetItem* item){
    if(item->checkState()){
        item->setCheckState(Qt::Unchecked);
        int i = 0;
        for(i = 0; i < reportOptionIncomeCategory.size(); i++)
            if(reportOptionIncomeCategory[i] == item->text())
                reportOptionIncomeCategory.erase(reportOptionIncomeCategory.begin() + i);
    }
    else{
        item->setCheckState(Qt::Checked);
        reportOptionIncomeCategory.push_back(item->text());
    }
}
void StackedDialog::priceConstraint(){
    reportToPrice->setMinimum(reportFromPrice->value());
    searchToPrice->setMinimum(searchFromPrice->value());
}
void StackedDialog::dateConstraint(){
    searchToDate->setMinimumDateTime(searchFromDate->dateTime());
}

void StackedDialog::on_accountName_textChanged(QString input){
    this->accountname = input.toStdString();
}
